package org.projectwithoutname.emf;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

public class EMFLoader {
	
	/**
	 * 
	 * @param path - like "website/My.website"
	 * @return
	 */
	static public EObject loadDomain(String path) {
		Resource resource = getResource(path);
		return  resource.getContents().get(0);
	}
	
	static public void saveDomain(EObject domain, String path)
	{
		Resource resource = getResource(path);
		resource.getContents().clear();
		resource.getContents().add(domain);
		try {
			resource.save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	static private Resource getResource(String path)
	{
		ResourceSet resSet = new ResourceSetImpl();
		return resSet.getResource(URI.createPlatformResourceURI(path), true);
	}
	
	@Deprecated
	static public void saveElement(EObject element, String path)
	{
		
	}

}
